﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Models;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice.Entities;
using BackEnd.Common;

namespace BackEnd.Controllers
{
    public class AuctionHouseSublocationController : Controller
    {
        //
        // GET: /AuctionHouseSublocation/

        [CustomAuthorize(Action = Constants.AdminActions.ListAuctionHouseSublocation)]
        public ActionResult Index()
        {
            ListAuctionHouseSublocationModel auctionHouseSublocationModel = new ListAuctionHouseSublocationModel();
            List<AuctionHouse> listAuctionHouse = new AuctionHouseBL().GetAll();
            listAuctionHouse.Insert(0, new AuctionHouse { Id = -1, Name = "All Auction House----" });
            auctionHouseSublocationModel.ListAuctionHouse = listAuctionHouse;
            return View(auctionHouseSublocationModel);
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateAuctionHouseSublocation)]
        public ActionResult Create()
        {
            ActionAuctionHouseSublocationModel auctionHouseSublocationModel = new ActionAuctionHouseSublocationModel();
            auctionHouseSublocationModel.ListAuctionHouse = new AuctionHouseBL().GetAll();
            return View(auctionHouseSublocationModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateAuctionHouseSublocation)]
        public ActionResult Create(ActionAuctionHouseSublocationModel auctionHouseSublocationModel)
        {
            if (string.IsNullOrWhiteSpace(auctionHouseSublocationModel.Name))
            {
                ModelState.AddModelError("Name", "Name is not null");
            }

            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new AuctionHouseSublocationBL().Insert(new AuctionHouseSublocation { Name = auctionHouseSublocationModel.Name, AuctionHouseId = auctionHouseSublocationModel.AuctionHouseId });
                return RedirectToAction("Index", "AuctionHouseSublocation");
            }
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditAuctionHouseSublocation)]
        public ActionResult Edit(int id)
        {
            ActionAuctionHouseSublocationModel auctionHouseSublocationModel = new ActionAuctionHouseSublocationModel();
            AuctionHouseSublocation auctionHouseSublocation = new AuctionHouseSublocationBL().GetById(id);
            auctionHouseSublocationModel.Id = auctionHouseSublocation.Id;
            auctionHouseSublocationModel.Name = auctionHouseSublocation.Name;
            auctionHouseSublocationModel.AuctionHouseId = auctionHouseSublocation.AuctionHouseId;
            auctionHouseSublocationModel.ListAuctionHouse = new AuctionHouseBL().GetAll();
            return View(auctionHouseSublocationModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.EditAuctionHouseSublocation)]
        public ActionResult Edit(ActionAuctionHouseSublocationModel auctionHouseSublocationModel)
        {
            if (string.IsNullOrWhiteSpace(auctionHouseSublocationModel.Name))
            {
                ModelState.AddModelError("Name", "Name is not null");
            }

            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new AuctionHouseSublocationBL().Update(new AuctionHouseSublocation { Id = auctionHouseSublocationModel.Id, Name = auctionHouseSublocationModel.Name, AuctionHouseId = auctionHouseSublocationModel.AuctionHouseId });
                return RedirectToAction("Index", "AuctionHouseSublocation");
            }
        }

        public JsonResult Search(string name, int auctionHouseID, int pageIndex, int pageSize)
        {
            int totalRecord = 0;
            List<AuctionHouseSublocationModel> listAuctionHouseSublocation = new AuctionHouseSublocationBL().Search(name, auctionHouseID, pageIndex, pageSize, out totalRecord);

            ListObjectModel<AuctionHouseSublocationModel> listObject = new ListObjectModel<AuctionHouseSublocationModel> { ListItem = listAuctionHouseSublocation, PageIndex = pageIndex, PageSize = pageSize, TotalRecord = totalRecord };
            return Json(listObject, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteAuctionHouseSublocation)]
        public JsonResult Delete(string listId)
        {            
            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new AuctionHouseSublocationBL().Delete(Convert.ToInt32(arrayId[i]));
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

    }
}
